<?php

namespace App\Http\Controllers\Admin;

use App\Http\Repository\PageRepository;
use App\Http\Servers\Admin\ImageServer;
use App\Http\Servers\Admin\PageServer;
use App\PageInfo;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PageController extends CommonController
{
    //标签列表
    public function pagelabel(Request $request, ImageServer $imageServer, PageServer $pageServer)
    {
        if ($request->has('del')) {
            $pagelabel = \App\PageLabel::findorfail($request->del);

            $sofeVerify = $pageServer->sofeVerify($request->del, $pagelabel->author);

            if ($sofeVerify) {
                $message['message'] = $sofeVerify;
                return back()->with($message);
            }

            $result = $pagelabel->delete();
            if ($result) {
                $imageServer->deleteImage($pagelabel->image);
                $message['message'] = message(true, '删除成功', '模块 ' . $pagelabel->name . ' 删除成功!');
            } else {
                $message['message'] = message(false, '删除失败', '模块 ' . $pagelabel->name . ' 删除失败!');
            }
            return back()->with($message);
        }

        $data['pageLabels'] = \App\PageLabel::orderBy('xindex', 'DESC')->get();
        $data['authors'] = \App\Admin::lists('name', 'id');

        return view('admin.page.pagelabel', $data);
    }

    //标签添加
    public function pagelabel_add(Request $request, ImageServer $imageServer, PageServer $pageServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'name' => 'required',
                'label' => 'required|alpha|unique:page_labels',
                'image' => 'image',
                'theme' => 'required|page_theme',
            ]);

            $create['name'] = $request->name;
            $create['label'] = $request->label;
            $create['xindex'] = $request->xindex;
            $create['theme'] = $request->theme;
            $create['author'] = \Auth::guard('admin')->user()->id;
            $create['status'] = $request->has('status') ? 1 : 0;
            if ($request->hasFile('image')) {
                $tmpImage = $imageServer->uploadImage($request->image, 'page_label', 'page_label');
                $tmpImage && $create['image'] = $tmpImage;
            }

            $result = \App\PageLabel::create($create);
            if ($result) {
                $message['message'] = message(true, '添加成功', '模块 ' . $result->name . ' 添加成功!');
            } else {
                $message['message'] = message(false, '添加失败', '模块 ' . $result->name . ' 添加失败!');
            }
            return back()->with($message);
        }

        $data['infoThemes'] = $pageServer->getInfoTheme();

        return view('admin.page.pagelabel_add',$data);
    }

    //模块编辑
    public function pagelabel_edit(Request $request, ImageServer $imageServer, PageServer $pageServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'label' => 'required|alpha|unique:page_labels,label,' . $request->id . ',id',
                'image' => 'image',
                'theme' => 'required|page_theme',
            ]);

            $author = \App\PageLabel::where('id', $request->id)->value('author');

            $nopower = $pageServer->nopower($author);
            if ($nopower) {
                $message['message'] = $nopower;
                return back()->with($message);
            }

            $update['name'] = $request->name;
            $update['label'] = $request->label;
            $update['xindex'] = $request->xindex;
            $update['theme'] = $request->theme;
            $update['status'] = $request->has('status') ? 1 : 0;
            if ($request->hasFile('image')) {
                $imageid = \App\PageLabel::where('id', $request->id)->value('image');
                $tmpImage = $imageServer->uploadImage($request->image, 'page_label', 'page_label');
                $tmpImage && $update['image'] = $tmpImage;
            }

            $result = \App\PageLabel::where('id', $request->id)->update($update);
            if ($result) {
                if ($request->hasFile('image')) {
                    $imageServer->deleteImage($imageid);
                }
                $message['message'] = message(true, '添加成功', '模块 ' . $request->name . ' 添加成功!');
            } else {
                $message['message'] = message(false, '添加失败', '模块 ' . $request->name . ' 添加失败!');
            }
            return back()->with($message);
        }

        $data['infoThemes'] = $pageServer->getInfoTheme();
        $data['pagelabel'] = \App\PageLabel::findorfail($request->id);
        $data['pagelabel']['image'] = \App\Image::where('id', $data['pagelabel']['image'])->value('path');

        return view('admin.page.pagelabel_edit', $data);
    }

    //企业信息列表
    public function pageinfo(Request $request, PageRepository $pageRepository, PageServer $pageServer, ImageServer $imageServer)
    {
        if ($request->has('del')) {
            $pageInfo = \App\PageInfo::findorfail($request->del);

            $nopower = $pageServer->nopower($pageInfo->author);
            if ($nopower) {
                $message['message'] = $nopower;
                return back()->with($message);
            }

            $result = $pageInfo->delete();
            if ($result) {
                $imageServer->deleteImage($pageInfo->image);
                $message['message'] = message(true, '删除成功', '删除企业信息 ' . $pageInfo->name . ' 成功!');
            } else {
                $message['message'] = message(false, '删除失败', '删除企业信息 ' . $pageInfo->name . ' 失败!');
            }
            return back()->with($message);
        }

        $data['pageInfos'] = $pageRepository->getPageinfoGroupPagelabel();
        $data['authors'] = \App\Admin::lists('name', 'id');

        return view('admin.page.pageinfo', $data);
    }

    //企业信息添加
    public function pageinfo_add(Request $request, PageServer $pageServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'name' => 'required',
                'theme' => 'required|page_theme',
                'page_view' => 'required',
                'tag' => 'required',
                'label' => 'required',
                'describe' => 'required',
                'image' => 'image',
                'jump_url' => 'url',
            ]);

            $create = $pageServer->getAddPageinfo($request);

            $result = \App\PageInfo::create($create);
            if ($result) {
                $message['message'] = message(true, '添加成功', '企业信息 ' . $result->name . ' 添加成功!');
            } else {
                $message['message'] = message(false, '添加失败', '企业信息 ' . $result->name . ' 添加失败!');
            }
            return back()->with($message);
        }

        $data['pageThemes'] = $pageServer->getPageinfoTheme();

        $data['pageLabels'] = \App\PageLabel::lists('name', 'id');

        $tmpTag = explode(',', config('set.web_keywords'));
        shuffle($tmpTag);
        $data['tag'] = $tmpTag[0];

        if ($request->has('label')) {
            $data['pageLabel'] = $request->label;
            $data['pageTheme'] = \App\PageLabel::where('id',$request->label)->value('theme');
        }

        return view('admin.page.pageinfo_add', $data);
    }

    //企业信息编辑
    public function pageinfo_edit(Request $request, PageServer $pageServer, ImageServer $imageServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'theme' => 'required|page_theme',
                'page_view' => 'required',
                'tag' => 'required',
                'label' => 'required',
                'describe' => 'required',
                'image' => 'image',
                'jump_url' => 'url',
            ]);

            $pageinfo = \App\PageInfo::findorfail($request->id);

            $update = $pageServer->getUpdatePageinfo($request);

            $result = \App\PageInfo::where('id', $request->id)->update($update);
            if ($result) {
                if ($request->hasFile('image')) {
                    $imageServer->deleteImage($pageinfo->image);
                }
                $message['message'] = message(true, '编辑成功', '企业信息 ' . $pageinfo->name . ' 编辑成功!');
            } else {
                $message['message'] = message(false, '编辑失败', '企业信息 ' . $pageinfo->name . ' 编辑失败!');
            }
            return back()->with($message);
        }

        $data['pageInfo'] = \App\PageInfo::findorfail($request->id);

        $data['pageThemes'] = $pageServer->getPageinfoTheme();

        $data['pageLabels'] = \App\PageLabel::lists('name', 'id');

        $data['pageInfo']['image'] = \App\Image::where('id', $data['pageInfo']['image'])->value('path');

        return view('admin.page.pageinfo_edit', $data);
    }
}